<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class MallGoodsTags extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.mall_goods_tags';

    protected $cacheModel = 'goods.mall_goods_tags';

    /**
     * 获取指定商品的标签
     * @param $gid
     * @param array $field
     * @return array|bool|mixed|string
     * @throws \Exception
     */
    public function getGoodsTags($gid, $field = [])
    {
        $tags = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $gid)->getDataAndSet(function () use ($gid)
        {
            try
            {
                $tags = $this->alias('t')
                    ->join('wechat_backup_07_18_14_43_00.mall_goods_tags_middle tm', 't.id=tm.tid')
                    ->where(['tm.gid' => $gid, 't.status' => 1])
                    ->column('t.name,t.des', 't.id');
            }
            catch (\Exception $e)
            {
                $tags = [];
            }
            return $tags;
        }, $field);

        if (is_string($tags))
        {
            $tags = json_decode($tags, true);
        }
        return $tags;
    }
}